<template>
	<view class="select-box">
		<view class="select-current" @click="openClose">
			<text class="current-name">{{current.name}}</text>
			<!-- <image class="curreny-img" src="https://cdn.spbcn.org/spbcnappneed/fengjihua_xiala.png"></image> -->
			<image class="curreny-img" src="../../static/image/fjh/fengjihua_xiala.png"></image>

		</view>
		<view class="option-list" v-if="isShow" @click="optionTap">
			<text class="option" :data-id="defaultOption.id" :data-name="defaultOption.name">{{defaultOption.name}}
			</text>
			<text class="option" v-for="(item, index) in result" :key="index" :data-id="item.id"
				:data-name="item.name">{{item.name}}
			</text>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			options: {
				type: Array,
				value: []
			},
			defaultOption: {
				type: Object,
			},
			selectkey: {
				type: String,
				value: 'id'
			},
			text: {
				type: String,
				value: 'name'
			}
		},
		data() {
			return {
				result: [],
				isShow: false,
				current: {}
			};
		},
		mounted() {
			console.log('options', this.options);
			// 属性名称转换, 如果不是 { id: '', name:'' } 格式，则转为 { id: '', name:'' } 格式
			let result = []
			if (this.selectkey !== 'id' || this.text !== 'name') {
				for (let item of this.options) {
					let {
						[this.selectkey]: id, [this.text]: name
					} = item
					result.push({
						id,
						name
					})
				}
			}
			this.current = Object.assign({}, this.defaultOption);
			this.result = result;
		},
		methods: {
			optionTap(e) {
				let dataset = e.target.dataset
				this.current = dataset;
				this.isShow = false;
				console.log('dataset________', dataset);
				// 调用父组件方法，并传参
				this.$emit("change", {
					...dataset
				});
				this.$emit("closeOther");
			},
			openClose() {
				this.isShow = !this.isShow;
				this.$emit("closeOther");
			},
			closePopview() {
				this.isShow = false;
			},
			// 此方法供父组件调用
			close() {
				this.isShow = false;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.select-box {
		position: relative;
		height: 100%;
		width: 100%;
	}

	.select-current {
		position: relative;
		width: 100%;
		padding: 0 5px;
		height: 25px;
		line-height: 25px;
		border: 1.5px solid #f19602;
		border-radius: 25px;
		box-sizing: border-box;
		background-color: white;
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.curreny-img {
		width: 25px;
		height: 14.5px;
	}

	/* .select-current::after {
	    position: absolute;
	    display: block;
	    right: 25rpx;
	    top: 20rpx;
	    content: '';
	    width: 0;
	    height: 0;
	    border: 10rpx solid transparent;
	    border-top: 10rpx solid #999;
	  } */

	.current-name {
		display: block;
		width: 85%;
		height: 100%;
		word-wrap: normal;
		color: black;
		overflow: hidden;
		text-align: center;
		font-size: 13px;
	}

	.option-list {
		position: absolute;
		left: 0;
		top: 27.5px;
		width: 100%;
		/* padding: 6px 10px 5px 10px; */
		border-radius: 3px;
		box-sizing: border-box;
		z-index: 99;
		box-shadow: 0rpx 0rpx 1rpx 1rpx rgba(0, 0, 0, 0.2) inset;
		background-color: #fff;
	}

	.option {
		display: block;
		width: 100%;
		line-height: 35px;
		color: black;
		border-bottom: 1rpx solid #eee;
		text-align: center;
	}

	.option:last-child {
		border-bottom: none;
		padding-bottom: 0;
	}
</style>